<template>
	<div>
		<el-dialog v-model="formVisible" :title="formTitle" width="80%" destroy-on-close :fullscreen='false'>
			<el-form class="formModel_form" ref="formRef" :model="form" label-width="$template2.back.add.form.base.labelWidth" :rules="rules">
				<el-row>
					<el-col :span="12">
						<el-form-item label="医生账号" prop="yishengzhanghao">
							<el-select
								class="list_sel"
								:disabled="!isAdd||disabledForm.yishengzhanghao?true:false"
								v-model="form.yishengzhanghao" 
								placeholder="请选择医生账号"
								@change="yishengzhanghaoChange">
								<el-option v-for="(item,index) in yishengzhanghaoLists" :label="item" :value="item">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="医生姓名" prop="yishengxingming">
							<el-input class="list_inp" v-model="form.yishengxingming" placeholder="医生姓名"
								 type="text" 								:readonly="!isAdd||disabledForm.yishengxingming?true:false" />
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="挂号费" prop="guahaofei">
							<el-input class="list_inp" v-model.number="form.guahaofei" placeholder="挂号费"
								 type="text" 								:readonly="!isAdd||disabledForm.guahaofei?true:false" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="最大预约号码数量" prop="yuyuehaomashuliang">
							<el-input class="list_inp" v-model.number="form.yuyuehaomashuliang" placeholder="最大预约号码数量"
								 type="text" 								:readonly="!isAdd||disabledForm.guahaofei?true:false" />
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="最大挂号数量" prop="zuidaguahaoshuliang">
							<el-input class="list_inp" v-model.number="form.zuidaguahaoshuliang" placeholder="最大挂号数量"
								 type="text" 								:readonly="!isAdd||disabledForm.guahaofei?true:false" />
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="诊室" prop="zhenshi">
							<el-input class="list_inp" v-model.number="form.zhenshi" placeholder="诊室"
								 type="text" 								:readonly="!isAdd||disabledForm.guahaofei?true:false" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="班次" prop="banci">
							<el-select
								class="list_sel"
								:disabled="!isAdd||disabledForm.banci?true:false"
								v-model="form.banci" 
								placeholder="请选择班次"
								>
								<el-option v-for="(item,index) in banciLists" :label="item"
									:value="item"
									>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="上班时间" prop="shangbanshijian">
							<el-date-picker
								class="list_date"
								v-model="form.shangbanshijian"
								format="YYYY-MM-DD HH:mm:ss"
								value-format="YYYY-MM-DD HH:mm:ss"
								type="datetime"
								:readonly="!isAdd||disabledForm.shangbanshijian?true:false"
								placeholder="请选择上班时间" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="下班时间" prop="xiabanshijian">
							<el-date-picker
								class="list_date"
								v-model="form.xiabanshijian"
								format="YYYY-MM-DD HH:mm:ss"
								value-format="YYYY-MM-DD HH:mm:ss"
								type="datetime"
								:readonly="!isAdd||disabledForm.xiabanshijian?true:false"
								placeholder="请选择下班时间" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="日期" prop="riqi">
							<el-date-picker
								class="list_date"
								v-model="form.riqi"
								format="YYYY 年 MM 月 DD 日"
								value-format="YYYY-MM-DD"
								type="datetime"
								:readonly="!isAdd||disabledForm.riqi?true:false"
								placeholder="请选择日期" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="备注" prop="beizhu">
							<el-input class="list_inp" v-model="form.beizhu" placeholder="备注"
								 type="text" 								:readonly="!isAdd||disabledForm.beizhu?true:false" />
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="管理账号" prop="guanlizhanghao">
							<el-input class="list_inp" v-model="form.guanlizhanghao" placeholder="管理账号"
								 type="text" 								:readonly="!isAdd||disabledForm.guanlizhanghao?true:false" />
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="管理姓名" prop="guanlixingming">
							<el-input class="list_inp" v-model="form.guanlixingming" placeholder="管理姓名"
								 type="text" 								:readonly="!isAdd||disabledForm.guanlixingming?true:false" />
						</el-form-item>
					</el-col>

				</el-row>
			</el-form>
			<template #footer v-if="isAdd||type=='logistics'||type=='reply'">
				<span class="formModel_btn_box">
					<el-button class="formModel_cancel" @click="closeClick">取消</el-button>
					<el-button class="formModel_confirm" type="primary" @click="save"
						>
						提交
					</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<script setup>
	import {
		reactive,
		ref,
		getCurrentInstance,
		nextTick,
		computed,
		defineEmits
	} from 'vue'
	const context = getCurrentInstance()?.appContext.config.globalProperties;	
	const emit = defineEmits(['formModelChange'])
	//基础信息
	const tableName = 'tiaobanshenqing'
	const formName = '调班申请'
	//基础信息
	//form表单
	const form = ref({})
	const disabledForm = ref({
		yishengzhanghao : false,
		yishengxingming : false,
		guahaofei : false,
		banci : false,
		shangbanshijian : false,
		xiabanshijian : false,
		riqi : false,
		beizhu : false,
		guanlizhanghao : false,
		guanlixingming : false,
	})
	const formVisible = ref(false)
	const isAdd = ref(false)
	const formTitle = ref('')
	//表单验证
	//匹配整数
	const validateIntNumber = (rule, value, callback) => {
		if (!value) {
			callback();
		} else if (!context?.$toolUtil.isIntNumer(value)) {
			callback(new Error("请输入整数"));
		} else {
			callback();
		}
	}
	//匹配数字
	const validateNumber = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isNumber(value)) {
			callback(new Error("请输入数字"));
		} else {
			callback();
		}
	}
	//匹配手机号码
	const validateMobile = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isMobile(value)) {
			callback(new Error("请输入正确的手机号码"));
		} else {
			callback();
		}
	}
	//匹配电话号码
	const validatePhone = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isPhone(value)) {
			callback(new Error("请输入正确的电话号码"));
		} else {
			callback();
		}
	}
	//匹配邮箱
	const validateEmail = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isEmail(value)) {
			callback(new Error("请输入正确的邮箱地址"));
		} else {
			callback();
		}
	}
	//匹配身份证
	const validateIdCard = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.checkIdCard(value)) {
			callback(new Error("请输入正确的身份证号码"));
		} else {
			callback();
		}
	}
	//匹配网站地址
	const validateUrl = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isURL(value)) {
			callback(new Error("请输入正确的URL地址"));
		} else {
			callback();
		}
	}
	const rules = ref({
		yishengzhanghao: [
		],
		yishengxingming: [
		],
		guahaofei: [
			{ validator: validateIntNumber, trigger: 'blur' },
		],
		banci: [
		],
		shangbanshijian: [
		],
		xiabanshijian: [
		],
		riqi: [
		],
		beizhu: [
		],
		guanlizhanghao: [
		],
		guanlixingming: [
		],
	})
	//表单验证
	
	const formRef = ref(null)
	const id = ref(0)
	const type = ref('')
	//医生账号列表
	const yishengzhanghaoLists = ref([])
	//班次列表
	const banciLists = ref([])
	//methods

	//获取唯一标识
	const getUUID =()=> {
      return new Date().getTime();
    }
	//重置
	const resetForm = () => {
		form.value = {
			yishengzhanghao: '',
			yishengxingming: '',
			guahaofei: '',
			banci: '',
			shangbanshijian: '',
			xiabanshijian: '',
			riqi: '',
			beizhu: '',
			guanlizhanghao: '',
			guanlixingming: '',
		}
	}
	//获取info
	const getInfo = (customTableName)=>{
		let temp = customTableName || tableName
		context?.$http({
			url: `${temp}/info/${id.value}`,
			method: 'get'
		}).then(res => {
			let reg=new RegExp('../../../file','g')
			form.value = res.data.data
			formVisible.value = true
		})
	}
	const crossRow = ref('')
	const crossTable = ref('')
	const crossTips = ref('')
	const crossColumnName = ref('')
	const crossColumnValue = ref('')
	//初始化
	const init=(formId=null,formType='add',formNames='',row=null,table=null,statusColumnName=null,tips=null,statusColumnValue=null)=>{
		resetForm()
			form.value.shangbanshijian = context?.$toolUtil.getCurDateTime()
			form.value.xiabanshijian = context?.$toolUtil.getCurDateTime()
			form.value.riqi = context?.$toolUtil.getCurDate()
		if(formId){
			id.value = formId
			type.value = formType
		}
		if(formType == 'add'){
			isAdd.value = true
			formTitle.value = '新增' + formName
			formVisible.value = true
		}else if(formType == 'info'){
			isAdd.value = false
			formTitle.value = '查看原排班'
			getInfo('yishengpaiban')
		}else if(formType == 'edit'){
			isAdd.value = true
			formTitle.value = '修改' + formName
			getInfo()
		}
		else if(formType == 'cross'){
			isAdd.value = true
			formTitle.value = formNames
			// getInfo()
			for(let x in row){
				if(x=='yishengzhanghao'){
					form.value.yishengzhanghao = row[x];
					disabledForm.value.yishengzhanghao = true;
					continue;
				}
				if(x=='yishengxingming'){
					form.value.yishengxingming = row[x];
					disabledForm.value.yishengxingming = true;
					continue;
				}
				if(x=='guahaofei'){
					form.value.guahaofei = row[x];
					disabledForm.value.guahaofei = true;
					continue;
				}
				if(x=='banci'){
					form.value.banci = row[x];
					disabledForm.value.banci = true;
					continue;
				}
				if(x=='shangbanshijian'){
					form.value.shangbanshijian = row[x];
					disabledForm.value.shangbanshijian = true;
					continue;
				}
				if(x=='xiabanshijian'){
					form.value.xiabanshijian = row[x];
					disabledForm.value.xiabanshijian = true;
					continue;
				}
				if(x=='riqi'){
					form.value.riqi = row[x];
					disabledForm.value.riqi = true;
					continue;
				}
				if(x=='beizhu'){
					form.value.beizhu = row[x];
					disabledForm.value.beizhu = true;
					continue;
				}
				if(x=='guanlizhanghao'){
					form.value.guanlizhanghao = row[x];
					disabledForm.value.guanlizhanghao = true;
					continue;
				}
				if(x=='guanlixingming'){
					form.value.guanlixingming = row[x];
					disabledForm.value.guanlixingming = true;
					continue;
				}
			}
			if(row){
				crossRow.value = row
			}
			if(table){
				crossTable.value = table
			}
			if(tips){
				crossTips.value = tips
			}
			if(statusColumnName){
				crossColumnName.value = statusColumnName
			}
			if(statusColumnValue){
				crossColumnValue.value = statusColumnValue
			}
			formVisible.value = true
		}

		context?.$http({
			url: `${context?.$toolUtil.storageGet('sessionTable')}/session`,
			method: 'get'
		}).then(res => {
			var json = res.data.data
			if(json.hasOwnProperty('guanlizhanghao')&& context?.$toolUtil.storageGet("role")!="管理员"){
				form.value.guanlizhanghao = json.guanlizhanghao
				disabledForm.value.guanlizhanghao = true;
			}
			if(json.hasOwnProperty('guanlixingming')&& context?.$toolUtil.storageGet("role")!="管理员"){
				form.value.guanlixingming = json.guanlixingming
				disabledForm.value.guanlixingming = true;
			}
		})
		context?.$http({
			url: `option/yisheng/yishengzhanghao`,
			method: 'get'
		}).then(res=>{
			yishengzhanghaoLists.value = res.data.data
		})
		//由上级字段带出不可改
		disabledForm.value.yishengxingming = true;
		banciLists.value = "早班,中班,晚班".split(',')
	}
	//初始化
	//声明父级调用
	defineExpose({
		init
	})
	//关闭
	const closeClick = () => {
		formVisible.value = false
	}
	const yishengzhanghaoChange=()=>{
		context?.$http({
			url: `follow/yisheng/yishengzhanghao?columnValue=` + form.value.yishengzhanghao,
			method: 'get'
		}).then(res=>{
			if(res.data.data.yishengxingming){
				form.value.yishengxingming = res.data.data.yishengxingming
			}
		})
	}
	//富文本
	const editorChange = (e,name) =>{
		form.value[name] = e
	}
	//提交
	const save=()=>{
		var table = crossTable.value
		var objcross = JSON.parse(JSON.stringify(crossRow.value))
		let crossUserId = ''
		let crossRefId = ''
		let crossOptNum = ''
		if(type.value == 'cross'){
			if(crossColumnName.value!=''){
				if(!crossColumnName.value.startsWith('[')){
					for(let o in objcross){
						if(o == crossColumnName.value){
							objcross[o] = crossColumnValue.value
						}
					}
					//修改跨表数据
					changeCrossData(objcross)
				}else{
					crossUserId = context?.$toolUtil.storageGet('userid')
					crossRefId = objcross['id']
					crossOptNum = crossColumnName.value.replace(/\[/,"").replace(/\]/,"")
				}
			}
		}
		formRef.value.validate((valid)=>{
			if(valid){
				if(crossUserId&&crossRefId){
					form.value.crossuserid = crossUserId
					form.value.crossrefid = crossRefId
					let params = {
						page: 1,
						limit: 1000, 
						crossuserid:form.value.crossuserid,
						crossrefid:form.value.crossrefid,
					}
					context?.$http({
						url: `${tableName}/page`,
						method: 'get', 
						params: params 
					}).then(res=>{
						if(res.data.data.total>=crossOptNum){
							context?.$toolUtil.message(`${crossTips.value}`,'error')
							return false
						}else{
							context?.$http({
								url: `${tableName}/${!form.value.id ? "save" : "update"}`,
								method: 'post', 
								data: form.value 
							}).then(res=>{
								emit('formModelChange')
								context?.$toolUtil.message(`操作成功`,'success',()=>{
									formVisible.value = false
								})
							})
						}
					})
				}else{
					context?.$http({
						url: `${tableName}/${!form.value.id ? "save" : "update"}`,
						method: 'post', 
						data: form.value 
					}).then(res=>{
						emit('formModelChange')
						context?.$toolUtil.message(`操作成功`,'success',()=>{
							formVisible.value = false
						})
					})
				}
			}
		})
	}
	//修改跨表数据
	const changeCrossData=(row)=>{
		context?.$http({
			url: `${crossTable.value}/update`,
			method: 'post',
			data: row
		}).then(res=>{})
	}
</script>
<style lang="scss" scoped>
	// 表单
	.formModel_form{
		border-radius: 6px;
		padding: 30px;
		box-shadow: 0 0px 0px rgba(85, 255, 255, 0.5);
		// form item
		:deep(.el-form-item) {
			border: 1px solid #ddd;
			margin: 0;
			display: flex;
			//label
			.el-form-item__label {
			 border: 1px solid #ddd;
			 padding: 0;
			 background: rgba(236,236,236,1);
			 display: block;
			 width: 120px;
			 border-width: 0 2px 0 0;
			 text-align: center;
			}
			// 内容盒子
			.el-form-item__content {
				padding: 0 20px;
				display: flex;
				width: calc(100% - 120px);
				justify-content: flex-start;
				align-items: center;
				flex-wrap: wrap;
				// 输入框
				.list_inp {
					border: 1px solid #ddd;
					box-shadow: 0 0 0px rgba(85, 255, 255, 0.5);
					padding: 0 10px;
					width: 100%;
					line-height: 36px;
					box-sizing: border-box;
					height: 36px;
					//去掉默认样式
					.el-input__wrapper{
						border: none;
						box-shadow: none;
						background: none;
						border-radius: 0;
						height: 100%;
						padding: 0;
					}
					.is-focus {
						box-shadow: none !important;
					}
				}
				//日期选择器
				.list_date {
					border: 1px solid #ddd;
					border-radius: 0;
					box-shadow: 0 0 0px rgba(85, 255, 255, 0.5);
					width: 100%;
					line-height: 36px;
					box-sizing: border-box;
					//去掉默认样式
					.el-input__wrapper{
						border: none;
						box-shadow: none;
						background: none;
						border-radius: 0;
						height: 100%;
					}
				}
				// 下拉框
				.list_sel {
					border: 1px solid #ddd;
					border-radius: 0;
					box-shadow: 0 0 0px rgba(85, 255, 255, 0.5);
					padding: 0 10px;
					width: 100%;
					line-height: 36px;
					box-sizing: border-box;
					//去掉默认样式
					.select-trigger{
						height: 100%;
						.el-input{
							height: 100%;
							.el-input__wrapper{
								border: none;
								box-shadow: none;
								background: none;
								border-radius: 0;
								height: 100%;
								padding: 0;
							}
							.is-focus {
								box-shadow: none !important;
							}
						}
					}
				}
			}
		}
	}
	// 按钮盒子
	.formModel_btn_box {
		padding: 40px 0 0;
		display: flex;
		width: 100%;
		justify-content: center;
		align-items: center;
		.formModel_cancel {
			border: 0;
			cursor: pointer;
			border-radius: 4px;
			padding: 0 24px;
			margin: 0 10px 0 0;
			outline: none;
			color: #fff;
			background: rgba(27, 78, 133, 1);
			width: auto;
			font-size: 14px;
			height: 32px;
		}
		.formModel_cancel:hover {
			background: rgba(27, 78, 133, 0.5);
		}
		
		.formModel_confirm {
			border: 0;
			cursor: pointer;
			border-radius: 4px;
			padding: 0 24px;
			margin: 0 10px 0 0;
			outline: none;
			color: #fff;
			background: rgba(241, 49, 100, 1);
			width: auto;
			font-size: 14px;
			height: 32px;
		}
		.formModel_confirm:hover {
			background: rgba(241, 49, 100, .5);
		}
	}
</style>